package com.gorkr.labuladong.data_struct.array.windows;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @author gorkr
 * @date 2022/06/07 10:20
 **/
public class M3LongestSubstringWithoutRepeatingCharacters {

    public int lengthOfLongestSubstring(String s) {
        // 滑动窗口找中间
        HashMap<Character, Integer> window = new HashMap<>();

        int meMax = 0; // 没有重复的情况呢
        int left = 0, right = 0;
        while(right<s.length()){
            char c = s.charAt(right);
            right++;
            window.put(c,window.getOrDefault(c,0)+1);

            while (window.get(c)==2){
                char b = s.charAt(left);
                left++;

                if(window.get(b)==1){
                    window.remove(b);
                }else {
                    window.put(b,1);
                }
            }
            // 对左指针理解不清。 做挑出来的时候c一定是1了
            meMax = Math.max(meMax,right-left);


        }


        return meMax;

    }

    @Test
    public void test(){
//        String s = "abcabcbb";
//        String s = s = "bbbbb";
//        String s = "pwwkew";
        String  s= "ohomm";
        System.out.println(lengthOfLongestSubstring(s));
    }
}
    